package clientGUI.adminGUI;

import javax.swing.*;

import clientApp.Course;
import clientApp.Plan;
import clientApp.ProjectConstants;
import clientApp.VirtualAdvisor;
import clientGUI.PassBackToable;
import clientGUI.SelectCoursesGUI_Catalog;
import clientGUI.studentGUI.GUI_Function_Library;
import clientGUI.studentGUI.SelectCoursesGUI_Plan;


import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

/*
 * severely needs some work
 */
public class AdminGUI implements PassBackToable, ActionListener{

	private VirtualAdvisor myVA;
	
	/**
	 * constructor
	 */
	public AdminGUI(VirtualAdvisor myVA)
	{
		this.myVA = myVA;
		JPanel mainPane = AdminMainPane();
		GUI_Function_Library.displayPane("Admin Main Page", mainPane, true);
	}
	
	/**
	 * @return
	 */
	private JPanel AdminMainPane()
	{
		JPanel mainPane = new JPanel(new GridLayout(1,3));

		// course
		JButton editCourseButton = new JButton("Edit Course");
		editCourseButton.setBorder(BorderFactory.createTitledBorder("Course"));
		editCourseButton.setActionCommand("course_edit");
		editCourseButton.addActionListener(this);
		
		// catalog
		JButton cat_viewButton		= new JButton("View Catalog");
		JButton cat_addCourseButton	= new JButton("Add Course(s)");
		JButton cat_remCourseButton	= new JButton("Remove Course(s)");
		//
		cat_viewButton.setActionCommand("cat_view");
		cat_addCourseButton.setActionCommand("cat_add");
		cat_remCourseButton.setActionCommand("cat_rem");
		cat_viewButton.addActionListener(this);
		cat_addCourseButton.addActionListener(this);
		cat_remCourseButton.addActionListener(this);
		//
		JPanel catalogPane = new JPanel(new GridLayout(3,1));
		catalogPane.setBorder(BorderFactory.createTitledBorder("Catalog"));
		catalogPane.add(cat_viewButton);
		catalogPane.add(cat_addCourseButton);
		catalogPane.add(cat_remCourseButton);
		
		// Degree Requirements
		// catalog
		JButton deg_viewButton		= new JButton("View Degrees");
		JButton deg_addCourseButton	= new JButton("Add Course(s)");
		JButton deg_remCourseButton	= new JButton("Remove Course(s)");
		//
		deg_viewButton.setActionCommand("deg_view");
		deg_addCourseButton.setActionCommand("deg_add");
		deg_remCourseButton.setActionCommand("deg_rem");
		deg_viewButton.addActionListener(this);
		deg_addCourseButton.addActionListener(this);
		deg_remCourseButton.addActionListener(this);
		//
		JPanel degreePane = new JPanel(new GridLayout(3,1));
		degreePane.setBorder(BorderFactory.createTitledBorder("Degree Requirements"));
		degreePane.add(deg_viewButton);
		degreePane.add(deg_addCourseButton);
		degreePane.add(deg_remCourseButton);
		
		// main
//		mainPane.add(new JLabel("PLEASE PARDON OUR DUST\nConstruction in Progress"));
		mainPane.add(editCourseButton);
		mainPane.add(catalogPane);
		mainPane.add(degreePane);
		
		return mainPane;
	}
	
	public void actionPerformed(ActionEvent e)
	{
		String cmd = e.getActionCommand();
		
		if(cmd.equals("course_edit"))
		{
			try {
				SelectCoursesGUI_Catalog selectCourseConsole = new SelectCoursesGUI_Catalog(this, cmd, true, myVA);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				GUI_Function_Library.exceptionHandler("AdminGUI:course_edit", (Exception)e1);
			}
		}
		else if(cmd.equals("cat_view"))
		{
			int height = 600;
			int width = 500;
			JFrame myFrame = new JFrame();
			myFrame.setSize(width, height);
			JPanel myTitlePanel = new JPanel(new GridLayout(1,6));
			myTitlePanel.setSize(500, 50);
			JPanel myMainPanel = new JPanel(new BorderLayout());
			myTitlePanel.add(new Label("CourseCode"));
			myTitlePanel.add(new Label("CourseName"));
			myTitlePanel.add(new Label("NumCreds"));
			myTitlePanel.add(new Label("Avail"));
			myTitlePanel.add(new Label("PreReqs"));
			
			ArrayList<Course> myCourses= myVA.getAllCourses();
			//ISSUES HERE
			final JPanel selectedCoursesPane = new JPanel(new GridLayout(myCourses.size(),6));
			selectedCoursesPane.setLayout(new BoxLayout(selectedCoursesPane, BoxLayout.PAGE_AXIS));
			
			for(Course cr : myCourses)
			{
				selectedCoursesPane.add(new Label("Course Code: " + cr.getCourseCode()));
				selectedCoursesPane.add(new Label("Course Name: " + cr.getCourseName()));
				selectedCoursesPane.add(new Label("Course Credits: " + Double.toString(cr.getNumCreds())));
				Label avail = new Label("");
				String me = "Availability:\n";
				for(Integer i : cr.getAvailability())
				{
					me += ProjectConstants.getSeasonString(i) + "\n";
				}
				avail.setText(me);
				String you = "Prereqs: ";
				for(String str : cr.getPrereqs())
				{
					you += str + ", ";
				}
				Label pre = new Label(you);
				selectedCoursesPane.add(avail);
				selectedCoursesPane.add(pre);
				selectedCoursesPane.add(new Label(""));
			}
			
			JScrollPane scrollSelectedPane = new JScrollPane(selectedCoursesPane);
			scrollSelectedPane.setPreferredSize(new Dimension(500, 550));
			
			
			
			//myMainPanel.add(myTitlePanel, BorderLayout.NORTH);
			myMainPanel.add(scrollSelectedPane, BorderLayout.CENTER);
			myFrame.add(myMainPanel);
			myFrame.setVisible(true);
			myFrame.pack();
		}
		else if(cmd.equals("cat_add"))
		{
			try {
//				SelectCoursesGUI_Catalog selectCourseConsole = new SelectCoursesGUI_Catalog(this, cmd, true, myVA);
				EditCourseGUI newCourseConsole = new EditCourseGUI(myVA);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				GUI_Function_Library.exceptionHandler("AdminGUI:course_edit", (Exception)e1);
			}
		}
		else if(cmd.equals("cat_rem"))
		{
			try {
				SelectCoursesGUI_Catalog selectCourseConsole = new SelectCoursesGUI_Catalog(this, cmd, true, myVA);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				GUI_Function_Library.exceptionHandler("AdminGUI:course_edit", (Exception)e1);
			}
		}
		else if(cmd.equals("deg_view"))
		{
			JOptionPane.showMessageDialog(null, "NOT IMPLEMENTED YET");
		}
		else if(cmd.equals("deg_add"))
		{
			try {
//				SelectCoursesGUI_Catalog selectCourseConsole = new SelectCoursesGUI_Catalog(this, cmd, true, myVA);
				JOptionPane.showMessageDialog(null, "NOT IMPLEMENTED YET");
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				GUI_Function_Library.exceptionHandler("AdminGUI:course_edit", (Exception)e1);
			}
		}
		else if(cmd.equals("deg_rem"))
		{
			try {
//				SelectCoursesGUI_Catalog selectCourseConsole = new SelectCoursesGUI_Catalog(this, cmd, false, myVA);
				SelectCoursesGUI_Plan selectCourseConsole = new SelectCoursesGUI_Plan(this, cmd, new Plan(""), myVA);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				GUI_Function_Library.exceptionHandler("AdminGUI:course_edit", (Exception)e1);
			}
		}
		else
		{
//			System.out.println("AdminGUI: BIZARRE COMMAND: " + cmd);
			myVA.terminate();
			System.exit(0);
		}
	}

	@Override
	public void passBack(String cmd, String message) throws Exception {
//		System.out.println(cmd);
//		System.out.println(message);
		
		if(cmd.equals("course_edit"))
		{
			EditCourseGUI editGUI = new EditCourseGUI((message.split(","))[1], myVA);
		}
		else if(cmd.equals("cad_add"))
		{
			JOptionPane.showMessageDialog(null, "Catalog Add: NOT IMPLEMENTED YET");
		}
		else if(cmd.equals("cat_rem"))
		{
			String[] data = message.split(",");
			for(int i = 0; i != data.length; i++)
			{
				myVA.removeCourseFromCatalog(data[i]);
			}
		}
		else if(cmd.equals("deg_add"))
		{
			JOptionPane.showMessageDialog(null, "Degree Add: NOT IMPLEMENTED YET");
		}
		else if(cmd.equals("deg_rem"))
		{
			String[] data = message.split(",");
			for(int i = 0; i != data.length; i++)
			{
				// REMOVE FROM DEGREE
			}
			JOptionPane.showMessageDialog(null, "Degree Remove: NOT IMPLEMENTED YET");
		}
		else
			JOptionPane.showMessageDialog(null, "AdminGUI: Functionality Not Yet Implemented: " + cmd);
//		System.out.println(myVA.getCourseFromCatalog("CMSC 201") == null);
	}
}
